<template>
	<view>
		<view v-if="buildworkercount != 0">
			<u-datetime-picker :show="Birthdayshow" mode="date" @cancel="Birthdayshow = false" @confirm="changeBirthday"
				:minDate="minDate" :closeOnClickOverlay="true" @close="Birthdayshow = false"></u-datetime-picker>
			<u-datetime-picker :show="InTimeshow" mode="date" @cancel="InTimeshow = false"
				@confirm="changeInTime" :closeOnClickOverlay="true" @close="InTimeshow = false"></u-datetime-picker>
			<u-datetime-picker :show="LeaveTimeshow" mode="date" @cancel="LeaveTimeshow = false"
				@confirm="changeLeaveTime" :closeOnClickOverlay="true" @close="LeaveTimeshow = false"></u-datetime-picker>
			<u-datetime-picker :show="InsureTimeshow" mode="date" @cancel="InsureTimeshow = false"
				@confirm="changeInsureTime" :closeOnClickOverlay="true" @close="InsureTimeshow = false"></u-datetime-picker>
			<u-datetime-picker :show="HighCardTimeshow" mode="date" @cancel="HighCardTimeshow = false"
				@confirm="changeHighCardTime" :closeOnClickOverlay="true" @close="HighCardTimeshow = false"></u-datetime-picker>
			<u-datetime-picker :show="EleCardTimeshow" mode="date" @cancel="EleCardTimeshow = false"
				@confirm="changeEleCardTime" :closeOnClickOverlay="true" @close="EleCardTimeshow = false"></u-datetime-picker>
			<u-datetime-picker :show="RecordTimeshow" mode="date" @cancel="RecordTimeshow = false"
				@confirm="changeRecordTime" :closeOnClickOverlay="true" @close="RecordTimeshow = false"></u-datetime-picker>
				
			<u-collapse ref='coll' :value="['1','2','3']">
				<u-collapse-item name="1" title="基本信息" icon="https://project.gxrunxiang.com/cdn/my.png">
					<u-form ref="uForm" label-width="auto">
						<u-form-item label="工号"><u-input v-model="buildworkerinfo.WorkNo" :disabled="true" /></u-form-item>
						<u-form-item label="姓名"><u-input v-model="buildworkerinfo.Name" :disabled="true" /></u-form-item>
						<u-form-item label="队长"><u-input v-model="buildworkerinfo.WorkTeam"
								:disabled="true" /></u-form-item>
						<u-form-item label="劳务公司"><u-input v-model="buildworkerinfo.WorkCompany"
								:disabled="true" /></u-form-item>
						<u-form-item label="证件类型"><u-input v-model="buildworkerinfo.IdCardType" /></u-form-item>
						<u-form-item label="证件号"><u-input v-model="buildworkerinfo.IdCardNo" /></u-form-item>
						<u-form-item label="国籍"><u-input v-model="buildworkerinfo.Country" /></u-form-item>
						<u-form-item label="性别"><u-input v-model="buildworkerinfo.Sex" /></u-form-item>
						<u-form-item label="手机号"><u-input v-model="buildworkerinfo.Cellphone" /></u-form-item>
						<u-form-item label="银行卡号"><u-input v-model="buildworkerinfo.BankCard" /></u-form-item>
						<u-form-item label="出生日期" @click="Birthdayshow = true"><u-input v-model="buildworkerinfo.Birthday"
								:disabled="true" /></u-form-item>
						<u-form-item label="在职状态"><u-input v-model="buildworkerinfo.WorkStatus" /></u-form-item>
					</u-form>
				</u-collapse-item>
				<u-collapse-item name="2" title="其他信息" icon="info-circle">
					<u-form ref="uForm" label-width="auto">
						<u-form-item label="入职时间" @click="InTimeshow = true"><u-input v-model="buildworkerinfo.InTime"
								:disabled="true" /></u-form-item>
						<u-form-item label="离职时间" @click="LeaveTimeshow = true"><u-input v-model="buildworkerinfo.LeaveTime"
								:disabled="true" /></u-form-item>
						<u-form-item label="劳动合同是否签订"><u-radio-group v-model="buildworkerinfo.IsContract">
								<u-radio label="是" :name="true"></u-radio>
								<u-radio label="否" :name="false"></u-radio>
							</u-radio-group></u-form-item>
						<u-form-item label="三级教育培训是否签订"><u-radio-group v-model="buildworkerinfo.IsLv3Train">
								<u-radio label="是" :name="true"></u-radio>
								<u-radio label="否" :name="false"></u-radio>
							</u-radio-group>
						</u-form-item>
						<u-form-item label="保密协议是否签订"><u-radio-group v-model="buildworkerinfo.IsSecrecy">
								<u-radio label="是" :name="true"></u-radio>
								<u-radio label="否" :name="false"></u-radio>
							</u-radio-group>
						</u-form-item>
						<u-form-item label="队长协议是否签订"><u-radio-group v-model="buildworkerinfo.IsCaptainContract">
								<u-radio label="是" :name="true"></u-radio>
								<u-radio label="否" :name="false"></u-radio>
							</u-radio-group>
						</u-form-item>
						<u-form-item label="安全交底是否签订"><u-radio-group v-model="buildworkerinfo.IsSafe">
								<u-radio label="是" :name="true"></u-radio>
								<u-radio label="否" :name="false"></u-radio>
							</u-radio-group>
						</u-form-item>
						<u-form-item label="是否购买保险"><u-radio-group v-model="buildworkerinfo.IsInsure">
								<u-radio label="是" :name="true"></u-radio>
								<u-radio label="否" :name="false"></u-radio>
							</u-radio-group>
						</u-form-item>
						<u-form-item label="保险单号"><u-input v-model="buildworkerinfo.InsureNo" /></u-form-item>
						<u-form-item label="保险有效期" @click="InsureTimeshow = true"><u-input
								v-model="buildworkerinfo.InsureTime" :disabled="true" /></u-form-item>
						<u-form-item label="高处作业证有效期" @click="HighCardTimeshow = true"><u-input
								v-model="buildworkerinfo.HighCardTime" :disabled="true" /></u-form-item>
						<u-form-item label="电工证有效期" @click="EleCardTimeshow = true"><u-input
								v-model="buildworkerinfo.EleCardTime" :disabled="true" /></u-form-item>
						<u-form-item label="备案时间" @click="RecordTimeshow = true"><u-input
								v-model="buildworkerinfo.RecordTime" :disabled="true" /></u-form-item>
						<u-form-item label="备注"><u-input v-model="buildworkerinfo.Remark" /></u-form-item>
					</u-form>
				</u-collapse-item>
				<u-collapse-item name="1" title="身份证" icon="https://project.gxrunxiang.com/cdn/image.png">
					<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple itype='身份证'
						:sizeType='["compressed"]' :maxCount="10"></u-upload>
						<u-album :urls="imagesurls1"></u-album>
				</u-collapse-item>
				<u-collapse-item name="2" title="高处作业证" icon="https://project.gxrunxiang.com/cdn/image.png">
					<u-upload :fileList="fileList2" @afterRead="afterRead" @delete="deletePic" name="2" multiple itype='高处作业证'
						:sizeType='["compressed"]' :maxCount="10"></u-upload>
						<u-album :urls="imagesurls2"></u-album>
				</u-collapse-item>
				<u-collapse-item name="3" title="电工证" icon="https://project.gxrunxiang.com/cdn/image.png">	
					<u-upload :fileList="fileList3" @afterRead="afterRead" @delete="deletePic" name="3" multiple itype='电工证'
						:sizeType='["compressed"]' :maxCount="10"></u-upload>
						<u-album :urls="imagesurls3"></u-album>
				</u-collapse-item>
				<u-collapse-item name="3" title="其他" icon="https://project.gxrunxiang.com/cdn/image.png">
					<u-upload :fileList="fileList4" @afterRead="afterRead" @delete="deletePic" name="4" multiple itype='其他'
						:sizeType='["compressed"]' :maxCount="10"></u-upload>
						<u-album :urls="imagesurls4"></u-album>
				</u-collapse-item>
			</u-collapse>
			<u-button @click="tosubmit">提交</u-button>
		</view>
		<u-empty
		        mode="data"
		        icon="https://project.gxrunxiang.com/cdn/list.png"
				v-if="buildworkercount == 0"
		>
		</u-empty>
	</view>
</template>
<script>
	import {
		baseUrl
	} from '@/common/config.js'
	import {
		mapGetters
	} from 'vuex'

	import {
		getworkerslist,
		getfilebyoutid,
		workerupdate
	} from '@/common/api.js'
	export default {
		data() {
			return {
				pageIndex: 1, //当前页数
				pageSize: 1, //页数大小
				buildworkerinfo: [],
				user: {},
				Birthdayshow: false,
				InTimeshow: false,
				LeaveTimeshow: false,
				InsureTimeshow: false,
				HighCardTimeshow: false,
				EleCardTimeshow: false,
				RecordTimeshow: false,
				fileList1: [],
				fileList2: [],
				fileList3: [],
				fileList4: [],
				imagesurls1: [],
				imagesurls2: [],
				imagesurls3: [],
				imagesurls4: [],
				workerName: "",
				minDate: -931144666000,
				buildworkercount: "",
				token: "",
				itypelist: {
					"1": "身份证",
					"2": "高处作业证",
					"3": "电工证",
					"4": "其他",
				},
			}
		},
		onLoad(option) {
			this.user = JSON.parse(uni.getStorageSync("$userinfo"))
			this.workerName=option.name
			this.getlist()
			this.token = uni.getStorageSync("$token")
		},
		methods: {
			changeBirthday(e) {
				const timeFormat = uni.$u.timeFormat
				this.buildworkerinfo.Birthday = timeFormat(e.value, 'yyyy-mm-dd')
				this.Birthdayshow = false
			},
			changeInTime(e) {
				const timeFormat = uni.$u.timeFormat
				this.buildworkerinfo.InTime = timeFormat(e.value, 'yyyy-mm-dd')
				this.InTimeshow = false
			},
			changeLeaveTime(e) {
				const timeFormat = uni.$u.timeFormat
				this.buildworkerinfo.LeaveTime = timeFormat(e.value, 'yyyy-mm-dd')
				this.LeaveTimeshow = false
			},
			changeInsureTime(e) {
				const timeFormat = uni.$u.timeFormat
				this.buildworkerinfo.InsureTime = timeFormat(e.value, 'yyyy-mm-dd')
				this.InsureTimeshow = false
			},
			changeHighCardTime(e) {
				const timeFormat = uni.$u.timeFormat
				this.buildworkerinfo.HighCardTime = timeFormat(e.value, 'yyyy-mm-dd')
				this.HighCardTimeshow = false
			},
			changeEleCardTime(e) {
				const timeFormat = uni.$u.timeFormat
				this.buildworkerinfo.EleCardTime = timeFormat(e.value, 'yyyy-mm-dd')
				this.EleCardTimeshow = false
			},
			changeRecordTime(e) {
				const timeFormat = uni.$u.timeFormat
				this.buildworkerinfo.RecordTime = timeFormat(e.value, 'yyyy-mm-dd')
				this.RecordTimeshow = false
			},
			async getlist() {
				// let querytype = this.$route.query.querytype
				let para = {
					Parameters: [],
				}
				para.Parameters.push({
					FieldName: "Name",
					ConditionalType: "Like",
					FieldValue: this.workerName || this.user.Name
					// FieldValue: '欧阳科'
					
				})
				para.OrderBys = ['CreateTime desc']
				para.pageSize = this.pageSize
				para.pageIndex = this.pageIndex
				await getworkerslist(para).then(res => {
					this.buildworkerinfo = res.data[0]
					this.buildworkercount = res.count
				})
				this.imagesurls1 = []
				this.imagesurls2 = []
				this.imagesurls3 = []
				this.imagesurls4 = []
				getfilebyoutid(this.buildworkerinfo.Id).then(res => {
					for (var i = 0; i < res.length; i++) {
						if (res[i].MyType == '身份证') {
							this.imagesurls1.push(`${baseUrl}/SysFile/download?id=${res[i].Id}`)
						}
						if (res[i].MyType == '高处作业证') {
							this.imagesurls2.push(`${baseUrl}/SysFile/download?id=${res[i].Id}`)
						}
						if (res[i].MyType == '电工证') {
							this.imagesurls3.push(`${baseUrl}/SysFile/download?id=${res[i].Id}`)
						}
						if (res[i].MyType == '其他') {
							this.imagesurls4.push(`${baseUrl}/SysFile/download?id=${res[i].Id}`)
						}
					}
					setTimeout(() => {
							// console.log(this.$refs.coll)
							this.$refs.coll.init()				
						},
						1000)
				})
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				console.log(event.name);
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url, this.itypelist[event.name])
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url, itype) {
				// console.log(this.user.Id)
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: `${baseUrl}/SysFile/upload?outid=${this.buildworkerinfo.Id||this.user.Id}&type=${itype}`,
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						header: {
							"Authorization": `Bearer ${this.token}`
						},
						success: (res) => {
							setTimeout(() => {
								resolve(res.data.data)
							}, 1000)
						}
					});
				})
			},
			tosubmit() {
				if(this.workerName){
					workerupdate(this.buildworkerinfo).then((res) => {
						if (res === true) {
							uni.redirectTo({
								url: "buildworkerinfo?name="+this.workerName
							})
						}
					})
				}else{
					workerupdate(this.buildworkerinfo).then((res) => {
						if (res === true) {
							uni.redirectTo({
								url: 'buildworkerinfo'
							})
						}
					})
				}
				
			}
		}
	}
</script>
<style>
	.cell-label {
		margin-top: 10px;
		font-size: 14px;
		color: #909193;
	}
</style>